package com.yadang.redisson;

import org.redisson.Redisson;
import org.redisson.api.RLock;

import java.util.concurrent.TimeUnit;
/**
 * @author: 亚当
 * @date: 2023年02月12日 11:56
 * @package: com.yadang.redisson
 * @Description: TODO
 */
public class RedissonLock {
    private Redisson redisson;
    private RLock lock;

    public RedissonLock(Redisson redisson, String name) {
        this.redisson = redisson;
        this.lock = redisson.getLock(name);
    }

    public boolean acquire(long waitTime, long leaseTime) {
        try {
            return lock.tryLock(waitTime, leaseTime, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return false;
        }
    }

    public void release() {
        if (lock.isHeldByCurrentThread()) {
            lock.unlock();
        }
    }
}
